//
//  MYGResumeEditController.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/12.
//

import UIKit
import BRPickerView
import IQKeyboardManagerSwift

class MYGResumeEditController: MYGBaseController {
    
    var hosArr:[String] = []
    var keshiArr:[String] = []
    
    private var phone = ""
    private var address = ""
    private var email = ""
    
    var learnS:[MYGResumeAllType] = []
    var workS:[MYGResumeAllType] = []
    
    private var array:[MYGResumType] = []
    private var contents: [Any] = []
    var image: UIImage?
    var index:Int = 1
    
    var model:MYGResumeModel? {
        didSet {
            guard let item = model else{return}
            
            self.phone = item.doctor.phoneNumber
            self.address = item.doctor.address
            self.email = item.doctor.email
            
            for listModel in item.extModel {
                let type = listModel.modelName
                for dict in listModel.list {
                    if (type == "Learnings"){//教育
                        learnS.append(dict)
                    } else if (type == "Works"){//工作
                        workS.append(dict)
                    } else {// 专业擅长、其他
                        array.append(listModel)
                    }
                }
            }
            
        }
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        getHosAndSub()//获取科室
        configUI()
    }
    
    //    override func backAction() {
    ////        super.backAction()
    //        contents.removeAll()
    //        switch self.index {
    //        case 1:
    //            self.navigationController?.popViewController(animated: true)
    //        case 2:
    //           configCell()
    //            tipView.snp.remakeConstraints { make in
    //                make.right.left.equalToSuperview()
    //                make.top.equalTo(topView.snp.bottom).offset(22)
    //                make.height.equalTo(0)
    //            }
    //            tableView.snp.remakeConstraints { make in
    //                make.top.equalTo(topView.snp.bottom).offset(10)
    //                make.left.right.equalToSuperview()
    //                make.bottom.equalTo(saveButton.snp.top)
    //            }
    //            index = 1
    //            tipView.isHidden = true
    //            topView.setPace(index: index)
    //        case 3:
    //            CongfigCellEducation()
    //            tipView.title = "教育经历"
    //            index = 2
    //            topView.setPace(index: 2)
    //        default:
    //            break
    //        }
    //    }
    
    // MARK: - Cell
    //MARK: - 基本信息
    func configCell() {
        var items: [Any] = []
        let image = MYGChangeEnterMessageImageCellModel()
        image.image = UIImage(named: "\(model!.doctor.avatar)")
        image.url = model?.doctor.avatar
        items.append(image)
        
        let nameItem = MYGChangeEnterMessageTextCellModel()
        nameItem.name = "姓名"
        nameItem.placeholder = "请输入姓名"
        nameItem.content = self.model!.doctor.name
        nameItem.isRequeired = false
        items.append(nameItem)
        
        let sexItem = MYGChangeEnterMessageTextCellModel()
        sexItem.name = "专业级别"
        sexItem.placeholder = "请输入专业级别"
        sexItem.content = model!.doctor.level
        sexItem.isRequeired = false
        sexItem.canEdit = false
        sexItem.clickAction = {[weak sexItem, weak self] in
            IQKeyboardManager.shared.resignFirstResponder()
            guard let sexItem = sexItem, let self = self else {
                return
            }
            BRStringPickerView.showPicker(withTitle: "请选择", dataSourceArr: ["院士","医学博士","医学硕士","医学学士","医学教授","医学副教授","硕士生导师","博士生导师","讲师"], select: 0) {[self] item in
                sexItem.content = item?.value ?? ""
                self.model?.doctor.level = item?.value ?? ""
                self.tableView.reloadData()
 
            }
        }
        
        items.append(sexItem)
        
        let hospitalItem = MYGChangeEnterMessageTextCellModel()
        hospitalItem.name = "所在医院"
        hospitalItem.placeholder = "请输入所在医院"
        hospitalItem.isRequeired = false
        hospitalItem.canEdit = false
        hospitalItem.content = model!.doctor.hospital
        hospitalItem.clickAction = {[weak hospitalItem, weak self] in
            IQKeyboardManager.shared.resignFirstResponder()
            guard let dateItem = hospitalItem, let self = self else {
                return
            }
            let vc = MYGHospitalViewController()
            vc.callHospitailName = {name in
                dateItem.content = name
                self.model?.doctor.hospital = name
                self.tableView.reloadData()
            }
            self.navigationController?.pushViewController(vc, animated: true)
        }
        items.append(hospitalItem)
        
        let dateItem = MYGChangeEnterMessageTextCellModel()
        dateItem.name = "科室"
        dateItem.placeholder = "请输科室名称"
        dateItem.content = model!.doctor.department
        dateItem.isRequeired = false
        dateItem.canEdit = false
        dateItem.clickAction = {[weak dateItem, weak self] in
            IQKeyboardManager.shared.resignFirstResponder()
            guard let dateItem = dateItem, let self = self else {
                return
            }
            BRStringPickerView.showPicker(withTitle: "请选择科室", dataSourceArr: self.keshiArr, select: 0) {[self] item in
                dateItem.content = item?.value ?? ""
                self.model?.doctor.department =  item?.value ?? ""
                self.tableView.reloadData()
 
            }
        }
        items.append(dateItem)
        
        let phoneItem = MYGChangeEnterMessageTextCellModel()
        phoneItem.name = "当前职位"
        phoneItem.placeholder = "请输入当前职位"
        phoneItem.content = model!.doctor.title
        phoneItem.isRequeired = false
        phoneItem.canEdit = false
        phoneItem.clickAction = {[weak phoneItem, weak self] in
            IQKeyboardManager.shared.resignFirstResponder()
            guard let dateItem = phoneItem, let self = self else {
                return
            }
            BRStringPickerView.showPicker(withTitle: "请选择职称", dataSourceArr: ["主任医师","副主任医师","主治医师","住院医师","医生助理"], select: 0) {[self] item in
                dateItem.content = item?.value ?? ""
                self.model!.doctor.title =  item?.value ?? ""
                self.tableView.reloadData()
 
            }
        }
        items.append(phoneItem)
        
        let addressItem = MYGChangeEnterMessageTextCellModel()
        addressItem.name = "详细地址"
        addressItem.placeholder = "请输入详细地址"
        addressItem.content = model!.doctor.address
        addressItem.isRequeired = false
        items.append(addressItem)
        
        let contactItem = MYGChangeEnterMessageTextCellModel()
        contactItem.name = "手机号"
        contactItem.placeholder = "请输入手机号"
        contactItem.content = "\(model!.doctor.phone)"
        contactItem.isRequeired = false
        items.append(contactItem)
        
        let emailItem = MYGChangeEnterMessageTextCellModel()
        emailItem.name = "邮箱"
        emailItem.placeholder = "请输入邮箱"
        emailItem.content = model!.doctor.email
        emailItem.isRequeired = false
        items.append(emailItem)
        
        contents.append(items)
        tableView.reloadData()
    }
    
    //MARK: - 教育经历
    func CongfigCellEducation () {
        contents.removeAll()
        
        for item in learnS {
            var items: [Any] = []
            let schoolItem = MYGChangeEnterMessageTextCellModel()
            schoolItem.name = "学校名称"
            schoolItem.placeholder = "请输入学校名称"
            schoolItem.isRequeired = false
            schoolItem.content = item.school
            schoolItem.key = "\(item.id )"
            items.append(schoolItem)
            
            let edItem = MYGChangeEnterMessageTextCellModel()
            edItem.name = "学历"
            edItem.placeholder = "请输入学历"
            edItem.isRequeired = false
            edItem.content = item.education
            edItem.canEdit = false
            edItem.clickAction =  {[weak edItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = edItem, let self = self else {
                    return
                }
                BRStringPickerView.showPicker(withTitle: "请选择", dataSourceArr: ["专科","本科","硕士研究生","博士研究生"], select: 0) {[self] item in
                    dateItem.content = item?.value ?? ""
                    //item.education =  item?.value ?? ""
                    self.tableView.reloadData()
     
                }
            }
            items.append(edItem)
            
            let subjectItem = MYGChangeEnterMessageTextCellModel()
            subjectItem.name = "专业"
            subjectItem.placeholder = "请输入专业名称"
            subjectItem.isRequeired = false
            subjectItem.content = item.major
            items.append(subjectItem)
            
            let timeItem = MYGChangeEnterMessageTextCellModel()
            timeItem.name = "开始时间"
            timeItem.placeholder = "请选择时间"
            timeItem.isRequeired = false
            timeItem.canEdit = false
            timeItem.content = item.yearStart
            timeItem.clickAction = {[weak timeItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = timeItem, let self = self else {
                    return
                }
                
                BRDatePickerView.showDatePicker(with: .YMD, title: "请选择日期", selectValue: "") { _, date in
                    dateItem.content = date ?? ""
                    self.tableView.reloadData()
                }
            }
            items.append(timeItem)
            
            let timeEndItem = MYGChangeEnterMessageTextCellModel()
            timeEndItem.name = "结束时间"
            timeEndItem.placeholder = "请选择时间"
            timeEndItem.isRequeired = false
            timeEndItem.canEdit = false
            timeEndItem.content = item.yearEnd
            timeEndItem.clickAction = {[weak timeEndItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = timeEndItem, let self = self else {
                    return
                }
                BRDatePickerView.showDatePicker(with: .YMD, title: "请选择日期", selectValue: "") { _, date in
                    dateItem.content = date ?? ""
                    self.tableView.reloadData()
                }
            }
            items.append(timeEndItem)
            
            
            let perItem = MYGChangeEnterMessageTextCellModel()
            perItem.name = "师承人"
            perItem.placeholder = "请输入姓名"
            perItem.isRequeired = false
            perItem.content = item.teacher
            items.append(perItem)
            
            let contactItem = MYGChangeEnterMessageTextCellModel()
            contactItem.name = "在校经历"
            contactItem.placeholder = "请输入在校经历"
            contactItem.isRequeired = false
            contactItem.content = item.experience
            items.append(contactItem)
            contents.append(items)
        }
        tableView.reloadData()
    }
    //MARK: - 工作经历
    func configCellWork(){
        
        contents = []
        for item in workS {
            var items: [Any] = []
            
            let hospitalItem = MYGChangeEnterMessageTextCellModel()
            hospitalItem.name = "医院/机构名称"
            hospitalItem.placeholder = "请输入医院/机构名称"
            hospitalItem.isRequeired = false
            hospitalItem.content = item.company
            hospitalItem.key = "\(item.id)"
            hospitalItem.canEdit = false
            hospitalItem.clickAction = {[weak hospitalItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = hospitalItem, let self = self else {
                    return
                }
                let vc = MYGHospitalViewController()
                vc.callHospitailName = {name in
                    dateItem.content = name
                    self.model?.doctor.hospital = name
                    self.tableView.reloadData()
                }
                self.navigationController?.pushViewController(vc, animated: true)
            }
            items.append(hospitalItem)
            
            let workItem = MYGChangeEnterMessageTextCellModel()
            workItem.name = "工作职称"
            workItem.placeholder = "请输入工作职称"
            workItem.isRequeired = false
            workItem.content = item.jobType
            workItem.canEdit = false
            workItem.clickAction = {[weak workItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = workItem, let self = self else {
                    return
                }
                BRStringPickerView.showPicker(withTitle: "请选择职称", dataSourceArr: ["主任医师","副主任医师","主治医师","住院医师","医生助理"], select: 0) {[self] item in
                    dateItem.content = item?.value ?? ""
                    self.model!.doctor.title =  item?.value ?? ""
                    self.tableView.reloadData()
     
                }
            }
            items.append(workItem)
            
            let timeItem = MYGChangeEnterMessageTextCellModel()
            timeItem.name = "在职时间"
            timeItem.placeholder = "请输入在职时间"
            timeItem.isRequeired = false
            timeItem.canEdit = false
            timeItem.content = item.dateStart
            timeItem.clickAction = {[weak timeItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = timeItem, let self = self else {
                    return
                }
                BRDatePickerView.showDatePicker(with: .YMD, title: "请选择日期", selectValue: "") { _, date in
                    dateItem.content = date ?? ""
                    self.tableView.reloadData()
                }
            }
            items.append(timeItem)
            
            let timeEndItem = MYGChangeEnterMessageTextCellModel()
            timeEndItem.name = "离职时间"
            timeEndItem.placeholder = "请输入离职时间"
            timeEndItem.isRequeired = false
            timeEndItem.canEdit = false
            timeEndItem.content = item.dateEnd
            timeEndItem.clickAction = {[weak timeEndItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = timeEndItem, let self = self else {
                    return
                }
                BRDatePickerView.showDatePicker(with: .YMD, title: "请选择日期", selectValue: "") { _, date in
                    dateItem.content = date ?? ""
                    self.tableView.reloadData()
                }
            }
            items.append(timeEndItem)
            
            
            let cItem = MYGChangeEnterMessageTextCellModel()
            cItem.name = "工作性质"
            cItem.placeholder = "请输入工作性质"
            cItem.isRequeired = false
            cItem.content = item.job_duty
            cItem.canEdit = false
            cItem.clickAction = { [weak cItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let cItem = cItem, let self = self else {
                    return
                }
                BRStringPickerView.showPicker(withTitle: "请选择", dataSourceArr:["全职","兼职","配偶","实习"], select: 0) { [self] item in
                    cItem.content = item?.value ?? ""
                    self.tableView.reloadData()
                }

            }
            items.append(cItem)
            
            let contentItem = MYGChangeEnterMessageTextCellModel()
            contentItem.name = "工作内容"
            contentItem.placeholder = "请输入工作内容"
            contentItem.isRequeired = false
            contentItem.content = item.content
            items.append(contentItem)
            contents.append(items)
        }
        tableView.reloadData()
    }
    
    // MARK: - UI
    private func configUI() {
        title = "编辑简历"
        
        view.backgroundColor = .white
        view.addSubview(tableView)
        view.addSubview(saveButton)
        //view.addSubview(tipView)
        view.addSubview(topView)
        
        topView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(18.5)
        }

        tableView.snp.makeConstraints { make in
                make.top.equalTo(topView.snp.bottom).offset(10)

            make.left.right.equalToSuperview()
            make.bottom.equalTo(saveButton.snp.top)
        }
        
        saveButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().inset(bottomSafeheight)
            make.height.equalTo(49)
        }
    }
    
    // MARK: - lazy load
    private lazy var topView:MYGResumePaceView = {
        let topV = MYGResumePaceView()
        return topV
    }()
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRectZero, style: .grouped)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .white
        tableView.register(cellWithClass: MYGChangeEnterMessageImageCell.self)
        tableView.register(cellWithClass: MYGChangeEnterMessageTitleCell.self)
        return tableView
    }()
    
    private lazy var saveButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("下一步", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .medium(17)
        //btn.backgroundColor = .rgba(162, 146, 254, 1)
        btn.addCAGradientLayerColor(colors: [UIColor.init(hexString: "#97B3FF").cgColor,
                                                 UIColor.init(hexString: "#4975EC").cgColor],
                                        startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint.init(x: 1, y: 0))
        btn.addTarget(self, action: #selector(nextAction), for: .touchUpInside)
        return btn
    }()
}

extension MYGResumeEditController {
    
    func secondStep (){
        index = 2
        CongfigCellEducation()
        topView.setPace(index: index)
    }
    
    func threeStep () {
        index = 3
        configCellWork()
        topView.setPace(index: index)
    }
    // MARK: - 基本信息
    func saveLearInfo(){
        MYGLoading.show()
        let arr = contents.first as! NSArray
        if let model = arr[0] as? MYGChangeEnterMessageImageCellModel, let image = model.image {
            MYGLoading.show()
            MMYGNetwork<MYGCommonAPI, MYGEmptyModel>.request(target: .uploadOssFiles(image: image)) { _ in
                MYGLoading.hide()
            } stringBlock: { text in
                self.saveDoctorInfo(avatar: text)
            } failed: { _, error in
                MYGLoading.hide()
                MMYGHUD.showToast(text: error)
            }
        } else {
            self.saveDoctorInfo(avatar: self.model?.doctor.avatar ?? "")
        }
    }
    
    func saveDoctorInfo(avatar:String){
        var params:[String:Any] = [:]
        params["user_id"] = self.model?.doctor.id
        params["avatar"] = avatar
        params["name"] = self.model?.doctor.name
        params["level"] = self.model?.doctor.level
        params["hospital"] = self.model?.doctor.hospital
        params["department"] = self.model?.doctor.department
        params["title"] = self.model?.doctor.title
        params["address"] = self.address
        params["email"] = self.email
        params["phone"] = self.phone
        
        MMYGNetwork<MYGRegisterAPI,MYGUserDoctor>.request(target:.uploadDoctorInf(id:"\(self.model?.doctor.id ?? 0)",params: params)) {[self] item in
            MYGLoading.hide()
            guard let model = item else{
                return
            }
//            let user = MYGUserManager.user!
//            user.doctor = model
//            MYGUserManager.saveUser(user)
            MMYGHUD.showToast(text: "保存成功")
//            secondStep()
//            tableView.snp.remakeConstraints { make in
//                make.top.equalTo(topView.snp.bottom).offset(10)
//                make.left.equalTo(0)
//                make.width.equalTo(screenWidth)
//                make.bottom.equalTo(saveButton.snp.top)
//            }
            getDoctorInfo()
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    
    func getDoctorInfo(){
        let phone = MYGUserManager.getUser()?.doctor.phone ?? 0
        MMYGNetwork<MYGMicrosrationApi,MYGUserDoctor>.request(target: .get_doctor_phone_info(phone: "\(phone)")) {[self] model  in
            MYGLoading.hide()
            guard let item = model else {return}
            
            let user = MYGUserManager.getUser()
            user?.doctor = item
            MYGUserManager.saveUser(user!)
            
            secondStep()
            tableView.snp.remakeConstraints { make in
                make.top.equalTo(topView.snp.bottom).offset(10)
                make.left.equalTo(0)
                make.width.equalTo(screenWidth)
                make.bottom.equalTo(saveButton.snp.top)
            }
         
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    
    // MARK: - 教育
    func saveWorkInfo(parames:[[String:String]]){
        MYGLoading.show()
        
        var params:[String:Any] = [:]
        params["doctor"] = [:]
        
        var content:[String:Any] = [:]
        content["modelName"] = "Learnings"
        content["list"] = parames
        content["index"] = ""
        
        params["extModel"] = [content]
        
        MMYGNetwork<MYGResumeApi,MYGEmptyModel>.request(target: .editMineResum(param: params)) {[self] _ in
            MYGLoading.hide()
            MMYGHUD.showToast(text: "保存成功")
            threeStep()
            
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    
    // MARK: - 工作
    func saveInfo(parames:[[String:String]]){
        MYGLoading.show()
        
        var params:[String:Any] = [:]
        params["doctor"] = [:]
        
        var content:[String:Any] = [:]
        content["modelName"] = "Works"
        content["list"] = parames
        content["index"] = ""
        
        params["extModel"] = [content]
        
        
        MMYGNetwork<MYGResumeApi,MYGEmptyModel>.request(target: .editMineResum(param: params)) {[self] _ in
            MYGLoading.hide()
            MMYGHUD.showToast(text: "保存成功")
            
            let vc = MYGResumeEditEndViewController()
            vc.skills = self.array
            vc.configCell(0)
            navigationController?.pushViewController(vc)
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
    }
    
    
    @objc private func nextAction() {
        
        if index == 1 {//   信息
            for subItem in contents {
                for (index, item) in(subItem as! NSArray).enumerated()  {
                    if let item = item as? MYGChangeEnterMessageTextCellModel {
                        switch index {
                        case 1:
                            self.model?.doctor.name = item.content
                        case 2:
                            self.model?.doctor.level = item.content
                        case 5:
                            self.model?.doctor.record = item.content
                        case 6:
                            self.address = item.content
                        case 7:
                            self.phone = item.content
                        case 8:
                            self.email = item.content
                        default:
                            break
                        }
                    }
                    
                }
            }
            saveLearInfo()
            
        } else if (index == 2) {//教育
            var arr:[[String: String]] = [[:]]
            arr.removeAll()
            for subItem in contents {
                var params: [String: String] = [:]
                for (index, item) in (subItem as! NSArray).enumerated() {
                    if let item = item as? MYGChangeEnterMessageTextCellModel {
                        switch index {
                        case 0:
                            params["school"] = item.content
                            if(!item.key.isEmpty){
                                params["id"] = item.key
                            }
                        case 1:
                            params["education"] = item.content
                        case 2:
                            params["major"] = item.content
                        case 3:
                            params["yearStart"] = item.content
                        case 4:
                            params["yearEnd"] = item.content
                        case 5:
                            params["teacher"] = item.content
                        case 6:
                            params["experience"] = item.content
                        default:
                            break
                        }
                    }
                }
                arr.append(params)
            }
                        
            saveWorkInfo(parames: arr)
            
        }else if (index == 3){//工作
            var arr:[[String: String]] = [[:]]
            arr.removeAll()
            for subItem in contents {
                var params: [String: String] = [:]
                for (index, item) in (subItem as! NSArray).enumerated() {
                    if let item = item as? MYGChangeEnterMessageTextCellModel {
                        if item.isRequeired && item.content.isEmpty {
                            MMYGHUD.showToast(text: "请信息填写完整")
                            return
                        }
                        
                        if item.content.isEmpty {
                            continue
                        }
                        
                        switch index {
                        case 0:
                            params["company"] = item.content
                            if(!item.key.isEmpty){
                                params["id"] = item.key
                            }
                        case 1:
                            params["jobType"] = item.content
                        case 2:
                            params["dateStart"] = item.content
                        case 3:
                            params["dateEnd"] = item.content
                        case 4:
                            params["job_duty"] = item.content
                        case 5:
                            params["content"] = item.content
                        default:
                            break
                        }
                    }
                }
                arr.append(params)
            }
            
            saveInfo(parames:arr)
            
        }
        
    }
 
}


extension MYGResumeEditController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        contents.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        let arr:NSArray = contents[section] as! NSArray
        return arr.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        65
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if index == 1 {
            let arr:[Any] = contents[indexPath.section] as! [Any]
            if let item = arr[indexPath.row] as? MYGChangeEnterMessageImageCellModel {
                let cell = tableView.dequeueReusableCell(withClass: MYGChangeEnterMessageImageCell.self, for: indexPath)
                cell.model = item
                return cell
            }
            
            if let item = arr[indexPath.row] as? MYGChangeEnterMessageTextCellModel {
                let cell = tableView.dequeueReusableCell(withClass: MYGChangeEnterMessageTitleCell.self, for: indexPath)
                cell.model = item
                return cell
            }
        }
        
        let arr:[Any] = contents[indexPath.section] as! [Any]
        if let item = arr[indexPath.row] as? MYGChangeEnterMessageTextCellModel {
            let cell = tableView.dequeueReusableCell(withClass: MYGChangeEnterMessageTitleCell.self, for: indexPath)
            cell.model = item
            return cell
        }
        
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if index == 1 {
            return UIView()
        }
        let v = UIView(frame: CGRect(x: 0, y: 0, width: Int(screenWidth), height: 30))
        v.backgroundColor = .white
        
        let line = UIView()
        line.backgroundColor = .rgba(73, 117, 236, 1)
        v.addSubview(line)
        
        let tipLab = UILabel()
        tipLab.text = (index == 2) ? "教育经历" : "工作经历"
        tipLab.textColor = .rgba(34, 34, 34, 1)
        tipLab.font = .medium(18)
        
        v.addSubview(tipLab)
        line.snp.makeConstraints { make in
            make.left.equalTo(17.5)
            make.height.equalTo(15)
            make.width.equalTo(3)
            make.centerY.equalToSuperview()
        }
        
        tipLab.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(line.snp.right).offset(13)
            make.height.equalTo(25)
        }
        
        if section == 0 {
            let addBtn = UIButton()
            addBtn.setImage(UIImage(named: "icon_tianjia"), for: .normal)
            addBtn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
            v.addSubview(addBtn)
            addBtn.snp.makeConstraints { make in
                make.top.bottom.right.equalToSuperview()
                make.width.equalTo(44)
            }
            
        }else {
            let btn = UIButton()
            btn.setImage(UIImage(named: "re_del"), for: .normal)
            btn.addTarget(self, action: #selector(delResumecontent(_:)), for: .touchUpInside)
            v.addSubview(btn)
            btn.tag = section
            btn.snp.makeConstraints { make in
                make.top.bottom.right.equalToSuperview()
                make.width.equalTo(44)
            }
        }

        return v
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if index == 1 {
            return 0
        }
        return 30
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        0
    }
}

extension MYGResumeEditController {
    // MARK: -  删除简历某一项
    @objc private func  delResumecontent(_ sender:UIButton) {
        let tag = sender.tag
        contents.remove(at: tag)
        tableView.reloadData()
    }
    
    @objc func clickButton() {
        if index == 2 {
            var items: [Any] = []
            
            let schoolItem = MYGChangeEnterMessageTextCellModel()
            schoolItem.name = "学校名称"
            schoolItem.placeholder = "请输入学校名称"
            schoolItem.isRequeired = false
            items.append(schoolItem)
            
            let edItem = MYGChangeEnterMessageTextCellModel()
            edItem.name = "学历"
            edItem.placeholder = "请输入学历"
            edItem.isRequeired = false
            edItem.canEdit = false
            edItem.clickAction =  {[weak edItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = edItem, let self = self else {
                    return
                }
                BRStringPickerView.showPicker(withTitle: "请选择", dataSourceArr: ["专科","本科","硕士研究生","博士研究生"], select: 0) {[self] item in
                    dateItem.content = item?.value ?? ""
                    //item.education =  item?.value ?? ""
                    self.tableView.reloadData()
     
                }
            }
            items.append(edItem)
            
            let subjectItem = MYGChangeEnterMessageTextCellModel()
            subjectItem.name = "专业"
            subjectItem.placeholder = "请输入专业名称"
            subjectItem.isRequeired = false
            items.append(subjectItem)
            
            let timeItem = MYGChangeEnterMessageTextCellModel()
            timeItem.name = "开始时间"
            timeItem.placeholder = "请选择时间"
            timeItem.isRequeired = false
            timeItem.canEdit = false
            timeItem.clickAction = {[weak timeItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = timeItem, let self = self else {
                    return
                }
                
                BRDatePickerView.showDatePicker(with: .YMD, title: "请选择日期", selectValue: "") { _, date in
                    dateItem.content = date ?? ""
                    self.tableView.reloadData()
                }
            }
            items.append(timeItem)
            
            let timeEndItem = MYGChangeEnterMessageTextCellModel()
            timeEndItem.name = "结束时间"
            timeEndItem.placeholder = "请选择时间"
            timeEndItem.isRequeired = false
            timeEndItem.canEdit = false
            timeEndItem.clickAction = {[weak timeEndItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = timeEndItem, let self = self else {
                    return
                }
                BRDatePickerView.showDatePicker(with: .YMD, title: "请选择日期", selectValue: "") { _, date in
                    dateItem.content = date ?? ""
                    self.tableView.reloadData()
                }
            }
            items.append(timeEndItem)
            
            
            let perItem = MYGChangeEnterMessageTextCellModel()
            perItem.name = "师承人"
            perItem.placeholder = "请输入姓名"
            perItem.isRequeired = false
            items.append(perItem)
            
            let contactItem = MYGChangeEnterMessageTextCellModel()
            contactItem.name = "在校经历"
            contactItem.placeholder = "请输入在校经历"
            contactItem.isRequeired = false
            items.append(contactItem)
            contents.append(items)
        }else {
            
            var items: [Any] = []
            
            let hospitalItem = MYGChangeEnterMessageTextCellModel()
            hospitalItem.name = "医院/机构名称"
            hospitalItem.placeholder = "请输入医院/机构名称"
            hospitalItem.isRequeired = false
            hospitalItem.canEdit = false
            hospitalItem.clickAction = {[weak hospitalItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = hospitalItem, let self = self else {
                    return
                }
                let vc = MYGHospitalViewController()
                vc.callHospitailName = {name in
                    dateItem.content = name
                    self.model?.doctor.hospital = name
                    self.tableView.reloadData()
                }
                self.navigationController?.pushViewController(vc, animated: true)
            }
            items.append(hospitalItem)
            
            let workItem = MYGChangeEnterMessageTextCellModel()
            workItem.name = "工作职称"
            workItem.placeholder = "请输入工作职称"
            workItem.isRequeired = false
            workItem.canEdit = false
            workItem.clickAction = {[weak workItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = workItem, let self = self else {
                    return
                }
                BRStringPickerView.showPicker(withTitle: "请选择职称", dataSourceArr: ["主任医师","副主任医师","主治医师","住院医师","医生助理"], select: 0) {[self] item in
                    dateItem.content = item?.value ?? ""
                    self.model!.doctor.title =  item?.value ?? ""
                    self.tableView.reloadData()
     
                }
            }
            items.append(workItem)
            
            let timeItem = MYGChangeEnterMessageTextCellModel()
            timeItem.name = "在职时间"
            timeItem.placeholder = "请输入在职时间"
            timeItem.isRequeired = false
            timeItem.canEdit = false
            timeItem.clickAction = {[weak timeItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = timeItem, let self = self else {
                    return
                }
                BRDatePickerView.showDatePicker(with: .YMD, title: "请选择日期", selectValue: "") { _, date in
                    dateItem.content = date ?? ""
                    self.tableView.reloadData()
                }
            }
            items.append(timeItem)
            
            let timeEndItem = MYGChangeEnterMessageTextCellModel()
            timeEndItem.name = "离职时间"
            timeEndItem.placeholder = "请输入离职时间"
            timeEndItem.isRequeired = false
            timeEndItem.canEdit = false
            timeEndItem.clickAction = {[weak timeEndItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let dateItem = timeEndItem, let self = self else {
                    return
                }
                BRDatePickerView.showDatePicker(with: .YMD, title: "请选择日期", selectValue: "") { _, date in
                    dateItem.content = date ?? ""
                    self.tableView.reloadData()
                }
            }
            items.append(timeEndItem)
            
            
            let cItem = MYGChangeEnterMessageTextCellModel()
            cItem.name = "工作性质"
            cItem.placeholder = "请输入工作性质"
            cItem.isRequeired = false
            cItem.canEdit = false
            cItem.clickAction = { [weak cItem, weak self] in
                IQKeyboardManager.shared.resignFirstResponder()
                guard let cItem = cItem, let self = self else {
                    return
                }
                BRStringPickerView.showPicker(withTitle: "请选择", dataSourceArr:["全职","兼职","配偶","实习"], select: 0) { [self] item in
                    cItem.content = item?.value ?? ""
                    self.tableView.reloadData()
                }

            }
            items.append(cItem)
            
            let contentItem = MYGChangeEnterMessageTextCellModel()
            contentItem.name = "工作内容"
            contentItem.placeholder = "请输入工作内容"
            contentItem.isRequeired = false
            items.append(contentItem)
            contents.append(items)
        }
        tableView.reloadData()
    }
    
    func getHosAndSub(){
        
        MMYGNetwork<MYGResumeApi, MYGSubjectListModel>.request(target: .getSubjectList(params: [:])) {[weak self] result in
            guard let items = result?.data else {
                return
            }
            for item in items {
                self?.keshiArr.append(item.label)
            }
        } failed: { _, error in
//            MYGLoading.hide()
//            MYGHUD.showToast(text: error)
        }
    }
}
